layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax', 'func', 'tree', 'util'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ZTree = layui.ztree;
    var $ax = layui.ax;
    var laydate = layui.laydate;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;
    var util = layui.util;

    form.render('select')

    /**
     * 用户管理--用户列表
     */
    var base = {
        tableId: "baseTable"    //表格id
    };

    //渲染时间选择框
    laydate.render({
        elem: '#timeLimit',
        range: true,
        max: Feng.currentDate()
    });

    /**
     * 初始化表格的列
     */
    base.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'mobile', align: "center", sort: true, title: '账号'},
            {field: 'nick_name', align: "center", sort: true, title: '名称'},
            {field: 'identityName', align: "center", sort: true, title: '类型'},
            {field: 'statusName', align: "center", sort: true, title: '是否实名',templet:function(d) {
            	var str = '';
            	if(d.status == 1){
            		str = '<p class="layui-btn layui-btn-yes-b layui-btn-xs">已实名</p>';
            	}else{
            		str = '<p class="layui-btn layui-btn-yes-a layui-btn-xs">未实名</p>';
            	}
            	return str;
            }},
            {field: 'company_name', align: "center", sort: true, title: '所属公司'},
            {field: 'create_time', align: "center", sort: true, title: '注册时间'},
            {field: 'update_time', align: "center", sort: true, title: '最后登录时间'},
            {fixed: 'right', toolbar: '#toolBtn', title: '操作', width: 200, align: 'center' }
        ]];
    };
    
    //绑定事件
    $(document).on('click', '#adopt', function(data) {
        var userId = $(this).attr("data-id");
        var status = $(this).attr("data-name");
    	if(status == '1'){
			titie ='确定要启用吗?';
		}else if(status == "0"){
			titie ='确定要停用吗?';
		}
		layer.confirm(titie, function() {
			//调用ajax局部启动
			$.ajax({
				url : Feng.ctxPath + "/modular/user/update",
				type : "POST",
				data : {
					"userId" : userId,
					"status" : status
				},
				dataType : "json",
				async : false,
				cache : false,
				success : function(obj) {
					if (obj != null) {
						if (obj.success) {
							layer.msg("操作成功");
						} else {
							layer.msg("操作失败");
						}
					}
				}
			});
			tableResult.reload();
			setTimeout("layer.close(layer.index);",2000);
		});
    });
    
    //绑定事件
    $(document).on('click', '#details', function(data) {
    	var userId = $(this).attr("data-id");
    	 func.open({
             type: 2,
             shadeClose: true,
             shade: 0.5,
             area: '300px',
             title: '查看详情',
             maxmin: true, //开启最大化最小化按钮
             content: Feng.ctxPath + "/modular/user/details/" + userId,
             tableId: base.tableId
         });
    });
    
    //绑定事件
    $(document).on('click', '#sealup', function(data) {
    	var userId = $(this).attr("data-id");
    	layer.open({
    		type: 2,
    		shadeClose: true,
    		shade: 0.5,
    		area: [480+'px', 350+'px'],
    		title: '封号',
    		maxmin: true, //开启最大化最小化按钮
    		content: Feng.ctxPath + "/modular/user/show?userId=" + userId,
    		tableId: base.tableId
    	});
    });


    /**
     * 点击查询按钮
     */
    base.search = function () {
        var queryData = {};
        queryData['userName'] = $("#userName").val();
        queryData['timeLimit'] = $("#timeLimit").val();
        queryData['userIdentity'] = $("#status").find("option:selected").val();
        table.reload(base.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 点击查看通知
     *
     * @param data 点击按钮时候的行数据
     */
    base.onQueryDetails = function (data) {
        func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '300px',
            title: '查看详情',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/tbuser/details/" + data.user_id,
            tableId: base.tableId
        });
    };

    // 用户订单列表
    base.onDeleteFeedback = function (data) {
    	func.open({
            type: 2,
            shadeClose: true,
            shade: 0.5,
            area: '800px',
            title: '用户订单列表',
            maxmin: true, //开启最大化最小化按钮
            content: Feng.ctxPath + "/modular/userinfo/userorderlist/" + data.user_id,
            tableId: base.tableId
        });
    }


    // 渲染表格
    var tableResult = table.render({
        title: "用户列表",
        elem: '#' + base.tableId,
        url: Feng.ctxPath + '/modular/user/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: base.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        base.search();
    });

    // 工具条点击事件
    table.on('tool(' + base.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'details') {
            base.onQueryDetails(data);
        } else if (layEvent === 'orderlist') {
            base.onDeleteFeedback(data);
        }
    });

    // 导出excel
    $('#btnExp').click(function () {
        base.exportExcel();
    });

    base.exportExcel = function () {
        var checkRows = table.checkStatus(base.tableId);

        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
});

